package com.iflytek.jcy.supervise.datastore.config;

import com.iflytek.jcy.supervise.datastore.api.DataSourceHolder;
import com.iflytek.jcy.supervise.datastore.api.JdbcTemplateHolder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/**
 * @author <a href=mailto:ktyi@iflytek.com>伊开堂</a>
 * @date 2022/10/12
 */
@EnableConfigurationProperties({DataStoreJdbcProperties.class})
public class DataStoreAutoConfiguration {

    @Bean
    JdbcTemplateHolder jdbcTemplateHolder(DataSourceHolder<?> dataSourceHolder, DataStoreJdbcProperties properties) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceHolder.getDataSource());
        DataStoreJdbcProperties.Template template = properties.getTemplate();
        jdbcTemplate.setFetchSize(template.getFetchSize());
        jdbcTemplate.setMaxRows(template.getMaxRows());
        if (template.getQueryTimeout() != null) {
            jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
        }
        return new JdbcTemplateHolder(new NamedParameterJdbcTemplate(jdbcTemplate));
    }


}
